package com.example.gomi.intelligentcitypipe.handler;

import android.media.SoundPool;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.example.gomi.intelligentcitypipe.DAO.PipeInfo;
import com.example.gomi.intelligentcitypipe.DAO.PipeInfoHelper;
import com.example.gomi.intelligentcitypipe.PipeMapActivity;
import com.example.gomi.intelligentcitypipe.serial.PipeProtocol;

import java.sql.SQLException;
import java.util.List;

public class MessageReceiveHandler extends Handler {


    private final static String TAG = "MessageReceiveHandler";

    private PipeMapActivity activity;


    private SoundPool mSoundPool;

    public MessageReceiveHandler(PipeMapActivity activity, SoundPool mSoundPool) {

        this.activity = activity;
        this.mSoundPool = mSoundPool;
    }


    private PipeInfo pipeInfo;


    @Override
    public void handleMessage(Message msg) {
        if (!(msg.obj instanceof PipeInfo)) {
            Log.e(TAG, "消息类型错误");
            return;
        }
        PipeInfo pipeInfo = (PipeInfo) msg.obj;
        switch (msg.what) {
            case PipeProtocol.COMMAND_BATTERY_LEVEL:
                int b = pipeInfo.getBatteryLevel();
                String batteylevel = "";
                switch (b) {
                    case PipeProtocol.BATTERY_FULL:
                        batteylevel = "full";
                        break;
                    case PipeProtocol.BATTERY_MEDIUM:
                        batteylevel = "medium";
                        break;
                    case PipeProtocol.BATTERY_LOW:
                        batteylevel = "low";
                        break;
                }
                Toast.makeText(activity, batteylevel, Toast.LENGTH_SHORT).show();
                Log.d("test", batteylevel);
                break;
            case PipeProtocol.COMMAND_READ_PAG11:

                if (activity.getAMapLocation() != null) {
                    pipeInfo.setLatitude(activity.getAMapLocation().getLatitude());
                    pipeInfo.setLongitude(activity.getAMapLocation().getLongitude());
                    pipeInfo.setGroupId("new____ON");
                    this.setPipeInfo(pipeInfo);
                } else {
                    Log.e("MessageReceiveHandler", "定位信息錯誤.");
                }
                Log.e("MessageReceiveHandler", "checkPipeID.");
                if (pipeInfo.checkPipeID()) {
                    Log.e("MessageReceiveHandler", "checkPipeID." + pipeInfo.toString());
                    Log.d("PipeMapActivity", pipeInfo.toString());
                    mSoundPool.play(1, 1, 1, 0, 0, 1);
                    Log.e("MessageReceiveHandler", "checkPipeID.");

                    try {
                        Log.e("MessageReceiveHandler", " PipeInfoHelper.getInstance().addPipe(pipeInfo);." + pipeInfo.toString());

                        PipeInfoHelper.getInstance().addPipe(pipeInfo);
                        activity.addNewPipInfo(pipeInfo);
                        Log.e("MessageReceiveHandler", " PipeInfoHelper.getInstance().addPipe(yyyyyyyyyyyyyyyyypipeInfo);." + pipeInfo.toString());
                        activity.updateMap(pipeInfo);
                        Toast.makeText(activity, "-------------------------------------------------------------------------------------->>>>>>>>>>>>>>>>>读取成功", Toast.LENGTH_SHORT).show();
                        Log.e("MessageReceiveHandler", "---------------------------------------------------------------------------->>>>>>>>>>>>>>>>>>>>>>>>>>>>读取成功." + pipeInfo.toString());
                    } catch (SQLException e) {
//                        Toast.makeText(PipeMapActivity.this,"标签已存在",Toast.LENGTH_SHORT).show();
//                        e.printStackTrace();
                        try {
                            PipeInfoHelper.getInstance().updatePipe(pipeInfo);
                            Toast.makeText(activity, "更新成功", Toast.LENGTH_SHORT).show();
                            activity.updateMap();
                        } catch (SQLException e1) {
                            e1.printStackTrace();
                        }
                    }
                } else {
                    Toast.makeText(activity, "ID检查失败", Toast.LENGTH_SHORT).show();
                    Log.e("MessageReceiveHandler", " PipeInfoHelper.getInstance().addPiID检查失败oooope(yyyyyyyyyyyyyyyyypipeInfo);." + pipeInfo.toString());
                }
                break;
            case PipeProtocol.SERIAL_NO_DATA:
                Toast.makeText(activity, "蓝牙未读取到数据", Toast.LENGTH_SHORT).show();
                Log.e("MessageReceiveHandler", " PipeInfoHelper.getInstanciiie().addPiID检查失败oooope(yyyyyyyyyyyyyyyyypipeInfo);." + pipeInfo.toString());
                break;
        }
    }

    public PipeInfo getPipeInfo() {
        return pipeInfo;
    }

    public void setPipeInfo(PipeInfo pipeInfo) {
        this.pipeInfo = pipeInfo;
    }
}
